package com.ybwh.springboot2.common;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Objects;

/**
 * ajax 返回给前端的数据 模型
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RespData<T> {

	/**
	 * 成功
	 */
	public static final Integer SUCCEED_CODE = 20000;
	/**
	 * 失败
	 */
	public static final Integer FAIL_CODE = 500;

	/**
	 * 重新登录
	 */
	public static final Integer RE_LOGIN_CODE = 50014;

	/**
	 * 状态码
	 */
	@ApiModelProperty(value="状态码；20000-成功，500-失败，50014-重新登录")
	private Integer code;

	/**
	 * 错误描述
	 */
	@ApiModelProperty(value="错误描述")
	private String msg;

	/**
	 * 调试的提示
	 */
	private String debugInfo;

	/**
	 * 返回数据
	 */
	@ApiModelProperty(value="返回数据")
	private T data;


	public RespData(Integer code, String msg, T data){
		this.code= code;
		this.msg = msg;
		this.data = data;
	}

	public boolean isSuccess(){
		return Objects.equals(SUCCEED_CODE , code) ;
	}

	public boolean isFailed(){
		return Objects.equals(FAIL_CODE , code) ;
	}

	public boolean isReLogin(){
		return Objects.equals(RE_LOGIN_CODE , code) ;
	}


	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}

	public static <E> RespData<E> successResp(E data) {
		return new RespData<E>(SUCCEED_CODE, null, data);
	}

	public static <E> RespData<E> failedResp(String msg) {
		return new RespData<E>(FAIL_CODE, msg, null);
	}


	public static <E> RespData<E> reLoginResp() {
		return new RespData<E>(RE_LOGIN_CODE, "请重新登录", null);
	}



}